package com.bensonbasement.snorflux.server;

import java.util.Collection;
import java.util.HashMap;

import com.bensonbasement.snorflux.server.verbs.Verb;
import com.bensonbasement.snorflux.server.verbs.WalkVerb;
import com.bensonbasement.snorflux.server.world.prototypes.ActorPrototype;
import com.bensonbasement.snorflux.server.world.prototypes.BigEntityPrototype;
import com.bensonbasement.snorflux.server.world.prototypes.SmallEntityPrototype;
import com.bensonbasement.snorflux.server.world.prototypes.SpacePrototype;
import com.bensonbasement.snorflux.server.world.prototypes.WorldElementPrototype;

public class ContentRepository {
	
	private static ContentRepository instance = null;
	
	public static ContentRepository getInstance() {
		if (instance == null) {
			instance = new ContentRepository();
		}
		return instance;
	}
	
	private final HashMap<String,WorldElementPrototype> prototypeMap;
	private final HashMap<String,Verb> objectlessVerbs;
	
	private ContentRepository() {
		objectlessVerbs = new HashMap<String,Verb>();
		prototypeMap = new HashMap<String,WorldElementPrototype>();
		
		// TODO: Move this somewhere more appropriate.
		addObjectlessVerb(WalkVerb.WALK);
	}
	
	
	// TODO: This is temporary.
	public void loadTestContent() {
		ActorPrototype playerPrototype = new ActorPrototype("test_player",ActorPrototype.ACTOR);
		playerPrototype.setName("spaceman");
		playerPrototype.setGlyph("spaceman2");
		BigEntityPrototype wallPrototype = new BigEntityPrototype("test_wall",BigEntityPrototype.BIGENTITY);
		wallPrototype.setName("wall");
		wallPrototype.setGlyph("wall");
		wallPrototype.setVisionBlocker(true);
		SpacePrototype spacePrototype = new SpacePrototype("test_floor",SpacePrototype.SPACE);
		spacePrototype.setName("floor");
		spacePrototype.setGlyph("floor2");
		SmallEntityPrototype raygunPrototype = new SmallEntityPrototype("test_raygun",SmallEntityPrototype.ITEM);
		raygunPrototype.setName("raygun");
		raygunPrototype.setGlyph("raygun");
		SmallEntityPrototype mopPrototype = new SmallEntityPrototype("test_mop",SmallEntityPrototype.ITEM);
		mopPrototype.setName("mop");
		mopPrototype.setGlyph("mop");
	}
	
	
	
	/**
	 * Should be called ONLY from the constructor for WorldElementPrototype.
	 * @param prototype
	 */
	public void addPrototype(WorldElementPrototype prototype) {
		if (prototypeMap.containsKey(prototype.getPrototypeID())) {
			throw new RuntimeException("Prototype already in content repository:"+prototype.getPrototypeID());
		}
		prototypeMap.put(prototype.getPrototypeID(), prototype);
	}
	
	public WorldElementPrototype getPrototype(String prototypeID) {
		WorldElementPrototype prototype = prototypeMap.get(prototypeID);
		if (prototype == null) {
			throw new RuntimeException("Prototype not found: "+prototypeID);
		}
		else {
			return prototype;
		}
	}
	
	public void addObjectlessVerb(Verb verb) {
		if (objectlessVerbs.containsKey(verb.getVerbName())) {
			throw new RuntimeException("Objectless verb already in content repository:"+verb.getVerbName());
		}
		objectlessVerbs.put(verb.getVerbName(), verb);
	}
	
	public Verb getObjectlessVerb(String verbName) {
		Verb verb = objectlessVerbs.get(verbName);
		if (verb == null) {
			throw new RuntimeException("Objectless Verb not found: "+verbName);
		}
		else {
			return verb;
		}
	}
	
	public Collection<Verb> getObjectlessVerbs() {
		return objectlessVerbs.values();
	}
}
